<?php

namespace App\Http\Controllers\Erp;

use App\CodeResponse;
use App\Repositories\Stock\StockTransferRepository;
use App\Services\Stock\StockTransferService;
use Illuminate\Http\Request;

class StockTransferController extends ErpController
{
    protected $stockTransferRepository;

    public function __construct(StockTransferRepository $stockTransferRepository)
    {
        $this->stockTransferRepository = $stockTransferRepository;

    }

    public function list(Request $request)
    {
        $input = $request->input();

        if(empty($input['is_all'])){
            $list = $this->stockTransferRepository->getList();
            return $this->paginate($list);
        }else{
            $list = $this->stockTransferRepository->getAll();
            return $this->success($list);
        }
    }

    public function detail($id)
    {
        $data = $this->stockTransferRepository->getById($id);
        return $this->success($data);

    }

    public function create(Request $request)
    {
        $params = $request->only(['out_storage_id','in_storage_id','remarks']);

        $flag = StockTransferService::getInstance()->createTransfer($params);
        return $this->failOrSuccess($flag, CodeResponse::CREATED_FAIL, $flag);

    }

    public function update($id, Request $request)
    {
        $params = $request->only(['out_storage_id','in_storage_id','remarks']);

        $flag = StockTransferService::getInstance()->updateTransfer($id,$params);
        return $this->failOrSuccess($flag, CodeResponse::UPDATED_FAIL, $flag);

    }

    //审核采购订单
    public function review($id)
    {
        $flag = StockTransferService::getInstance()->review($id);
        return $this->failOrSuccess($flag, CodeResponse::REVIEW_FAIL);

    }

    //取消采购订单
    public function cancel($id)
    {
        $flag = StockTransferService::getInstance()->cancelTransfer($id);
        return $this->failOrSuccess($flag, CodeResponse::CANCEL_FAIL, $flag);
    }

}